//
//  LLEmoticonToolbar.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/7/4.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

let tagNum = 1000

enum LLEmoticonToolbarType: Int{
    
    // 默认
    case Default = 0
    // Emoji表情
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}

class LLEmoticonToolbar: UIStackView {
    
    var  currentButton: UIButton?
    
    var toolbarClosure: ((type: LLEmoticonToolbarType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///设置视图
    func setupUI(){
        
        //排列方式
        axis = .Horizontal
        
        //填充方式（子控件大小相等，填充父控件）
        distribution = .FillEqually
        
       let leftButton = creatButton("left", title: "默认", type: .Default)
       let midButton = creatButton("mid", title: "Emoji", type: .Emoji)
       let rightButton = creatButton("right", title: "浪小花", type: .Lxh)
        
//        leftButton.snp_makeConstraints { (make) -> Void in
//            
//            make.leading.bottom.top.equalTo(self)
//            make.width.equalTo(midButton)
//        }
//        midButton.snp_makeConstraints { (make) -> Void in
//            
//            make.top.bottom.equalTo(self)
//            make.leading.equalTo(leftButton.snp_trailing)
//            make.width.equalTo(rightButton)
//        }
//        
//        rightButton.snp_makeConstraints { (make) -> Void in
//            
//            make.top.trailing.bottom.equalTo(self)
//            make.leading.equalTo(midButton.snp_trailing)
//        }
        
        leftButton.selected = true
        
        currentButton = leftButton
        
    }
    
    func setCurrontButtons(tag: Int){
        
        let button = viewWithTag(tag + tagNum) as! UIButton
        
        if currentButton == button {
            
            return
        }else{
            
            button.selected = true
            
            currentButton?.selected = false
            
            currentButton = button
        }
        
    }
    
    //监听toolbar按钮点击事件
    func emoticonButtonClick(button: UIButton){
        
        if currentButton == button{
            
            return
        }
            
            button.selected = true
            
            currentButton?.selected = false
            
            currentButton = button
            
        
        //执行闭包
        toolbarClosure?(type: LLEmoticonToolbarType(rawValue: button.tag - tagNum)!)
    }
    
    //创建button
    private func creatButton(imageName: String,title: String,type:LLEmoticonToolbarType) -> UIButton{
        
        let button = UIButton()
        
        button.tag = type.rawValue + tagNum
    button.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        
        button.setTitle(title, forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.addTarget(self, action: "emoticonButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
//        addSubview(button)
        self.addArrangedSubview(button)
        return button
    }
}
